import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_book/data/model/book_chapter_model.dart';
import 'package:flutter_book/page/book/animation_util/util/text_composition_analysis.dart';
import 'package:flutter_book/page/book/controller/book_page_controller.dart';
import 'package:flutter_book/page/test/book/first_chapter.dart';
import 'package:flutter_book/util/screen_util.dart';
import '../util/touch_event.dart';
import 'package:get/get.dart';
import 'dart:ui' as ui;
import 'package:flutter_book/util/extensions/string_extensions.dart';

abstract class BaseAnimationPage<T> {
  Offset mTouch = Offset(0, 0);

  late AnimationController mAnimationController;

  late BookPageController bookPageController;

  Size currentSize = Size(Get.width, Get.height);

  int currentPageIndexOld = 0;
  int currentPageIndex = 0;

  double downTouchClickX = 0;
  double downTouchClickY = 0;

  late TextComposition textCompositionCurrent;

  TextComposition? textCompositionPre;

  TextComposition? textCompositionNext;

  var physicalSize = window.physicalSize,
      ratio = window.devicePixelRatio,
      size = TextEditingController(text: '16'),
      height = TextEditingController(text: '1.55'),
      paragraph = TextEditingController(text: '10'),
      shouldJustifyHeight = true,
      start = DateTime.now(),
      end = DateTime.now(),
      showMenu = true;

  //点击事件
  Function? onTap;

  //长按事件
  Function? onLongTap;

  onTapListener(Function? onTap) {
    this.onTap = onTap;
  }

  onLongTapListener(Function? onLongTap) {
    this.onLongTap = onLongTap;
  }

  void setSize(Size size) {
    currentSize = size;
  }

  setBookContentController(BookPageController bookPageController) async {
    this.bookPageController = bookPageController;
    textCompositionCurrent = (await initTextCompositionIndex(
        bookPageController.getCurrentChapterParagraphs(), bookPageController.getCurrentChapter()?.title ?? "", true))!;
    initTextComposition();
  }

  void onDraw(Canvas canvas);

  void onTouchEvent(TouchEvent event);

  void setAnimationController(AnimationController controller) {
    mAnimationController = controller;
  }

  bool isShouldAnimatingInterrupt(){
    return false;
  }

  bool isCanGoNext(){
    return currentPageIndex < textCompositionCurrent.pageCount - 1 || textCompositionNext != null;
  }

  bool isCanGoPre(){
    return currentPageIndex > 0 || textCompositionPre != null;
  }


  bool? isCancelArea();

  bool? isConfirmArea();

  Animation<Offset>? getCancelAnimation(AnimationController controller, GlobalKey canvasKey);

  Animation<Offset>? getConfirmAnimation(AnimationController controller, GlobalKey canvasKey);

  Simulation? getFlingAnimationSimulation(AnimationController controller, DragEndDetails details);


  initTextComposition() async {
    textCompositionPre = await initTextCompositionIndex(
        bookPageController.getPreChapterParagraphs(),
        bookPageController
            .getPreChapter()
            ?.title ?? "");
    textCompositionNext = await initTextCompositionIndex(
        bookPageController.getNextChapterParagraphs(),
        bookPageController
            .getNextChapter()
            ?.title ?? "");
  }


  Future<TextComposition?> initTextCompositionIndex(List<String> paragraphs,
      String title,
      [bool isCurrentPage = false]) async {
    physicalSize = window.physicalSize;
    ratio = window.devicePixelRatio;
    final width = physicalSize.width / ratio;
    start = DateTime.now();
    TextComposition? textComposition;
    if (paragraphs.length > 0 || isCurrentPage) {
      textComposition = TextComposition(
        paragraphs: paragraphs,
        style: TextStyle(
          color: Colors.black87,
          fontSize: double.tryParse(size.text),
          height: double.tryParse(height.text),
        ),
        title: title,
        titleStyle: TextStyle(
          color: Colors.black87,
          fontWeight: FontWeight.bold,
          fontSize: double.tryParse(size.text),
          height: 2,
        ),
        titlePadding: 20,
        columnGap: 40,
        columnCount: width > 1200
            ? 3
            : width > 500
            ? 2
            : 1,
        paragraph: double.tryParse(paragraph.text) ?? 10.0,
        padding: EdgeInsets.only(top: ScreenUtil.instance.statusBarHeight + 10,
            bottom: 20, left: 10, right: 10),
        shouldJustifyHeight: shouldJustifyHeight,
        debug: false,
      );
    }
    end = DateTime.now();
    return textComposition;
  }

  Picture getCurrentPage() {
    if (currentPageIndex >= 0 && currentPageIndex < textCompositionCurrent.pageCount) {
      return textCompositionCurrent.getPicture(currentPageIndex);
    } else {
      return textCompositionCurrent.getPicture(0);
    }
  }

  Picture getPerPage() {
    int currentPageIndex = this.currentPageIndex - 1;
    if (currentPageIndex >= 0 && currentPageIndex < textCompositionCurrent.pageCount) {
      return textCompositionCurrent.getPicture(currentPageIndex);
    } else if (textCompositionPre != null) {
      return textCompositionPre!.getPicture(textCompositionPre!.pageCount - 1);
    } else {
      return textCompositionCurrent.getPicture(0);
    }
  }

  Picture getNextPage() {
    int currentPageIndex = this.currentPageIndex + 1;
    if (currentPageIndex >= 0 && currentPageIndex < textCompositionCurrent.pageCount) {
      return textCompositionCurrent.getPicture(currentPageIndex);
    } else if (textCompositionNext != null) {
      return textCompositionNext!.getPicture(0);
    } else {
      return textCompositionCurrent.getPicture(0);
    }
  }


  isSwitchTextData() {
    if (currentPageIndex < 0) {
      if (textCompositionPre != null) {
        textCompositionNext = textCompositionCurrent;
        textCompositionCurrent = textCompositionPre!;
        textCompositionPre = null;
        currentPageIndex = textCompositionCurrent.pageCount - 1;
        "切换到上一章$currentPageIndex,${textCompositionCurrent.pageCount}".print;
        setPreTextComposition();
      } else {
        currentPageIndex = 0;
        "没有上一章了".print;
      }
    } else if (currentPageIndex == textCompositionCurrent.pageCount) {
      if (textCompositionNext != null) {
        textCompositionPre = textCompositionCurrent;
        textCompositionCurrent = textCompositionNext!;
        textCompositionNext = null;
        currentPageIndex = 0;
        "切换到下一章$currentPageIndex,${textCompositionCurrent.pageCount}".print;
        setNextTextComposition();
      } else {
        currentPageIndex = textCompositionCurrent.pageCount - 1;
        "没有下一章了".print;
      }
    }
  }

  setPreTextComposition() async {
    textCompositionPre = await getPreTextComposition();
  }

  //获取上一章的数据
  Future<TextComposition?> getPreTextComposition() async {
    if (bookPageController.isHavePreChapter()) {
      await bookPageController.previousChapterContent();

      return await initTextCompositionIndex(
          bookPageController.getPreChapterParagraphs(),
          bookPageController
              .getPreChapter()
              ?.title ?? "");
    } else {
      "没有办法获取上一章的数据，没有上一章了".print;
    }
    return null;
  }


  setNextTextComposition() async {
    textCompositionNext = await getNextTextComposition();
  }

  //获取下一章的数据
  Future<TextComposition?> getNextTextComposition() async {
    if (bookPageController.isHaveNextChapter()) {
      await bookPageController.nextChapterContent();
      return await initTextCompositionIndex(
          bookPageController.getNextChapterParagraphs(),
          bookPageController
              .getNextChapter()
              ?.title ?? "");
    } else {
      "没有办法获取下一章的数据，没有下一章了".print;
    }
    return null;
  }


}

enum ANIMATION_TYPE { TYPE_CONFIRM, TYPE_CANCEL,TYPE_FILING }
